<?php

namespace app\storecheck\logic;

use think\facade\Db;
use think\facade\Cache;
use util\Excel;
use think\exception\ValidateException;
use think\facade\Log;
use util\Token;
use util\Upload;
use think\cache\driver\Redis;

class DailyQuantityLogic extends BasicLogic
{

    public function __construct()
    {
        
    }

    /**
     * @param $params
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     * @desc 列表
     * @author zhangheng
     */
    public function index($params = [])
    {
        $spu     = $params['spu'] ?? '';
        $limit = $params['limit'] ?? 10;
        $where = [];
        if($spu){
            $where[] = ['spu', 'like', '%' . $spu . '%'];
        }
        $list = Db::name('feishu_daily_quantity')
            ->where($where)
            ->order('Inventory desc')
            ->paginate($limit);

        $lists = $list->items();

        $data = [
            'list'  => $lists,
            'total' => $list->total(),
        ];
        return json(DefaultResponse(200, true, '获取成功', $data));
    }

    public function add($data = [])
    {
        if (!$data) {
            return json(DefaultResponse(201, false, '参数不能为空!', null));
        }
        $result = false;
        Db::startTrans();
        try {
            //广告账户
            // $result = Db::name('advaccount')->insert($AdvertisingAccount);
            if ($result !== false) { 
                Db::commit();
                return json(DefaultResponse(200, true, '添加成功!', null));
            }
            Db::rollback();
            return json(DefaultResponse(201, false, '更新失败!', null));
        } catch (ValidateException $e) {
            Db::rollback();
            return json(DefaultResponse(201, false, $e->getError(), null));
        } catch (\Exception $e) {
            Db::rollback();
            return json(DefaultResponse(201, false, '广告账户已存在!', null));
        }
        return json(DefaultResponse(200, false, '更新成功!', null));

    }

    public function find($id)
    {
        $where['id'] = $id;
        $row = Db::name('conversations')
            ->where($where)
            ->find();
        if (empty($row)) {
            return json(DefaultResponse(201, false, '数据不存在!', null));
        }
        return json(DefaultResponse(200, true, '获取成功', $row));
    }

    public function edit($data = [])
    {
        if (!$data) {
            return json(DefaultResponse(201, false, '参数不能为空!', null));
        }
        $id = $data['id'];
        // $row = $this->model->find($id);
        // if (empty($row)) {
        //     return json(DefaultResponse(201, false, '数据不存在!', null));
        // }
        $result = false;
        Db::startTrans();
        try {
            // $result = $row->save($AdvertisingAccount);
            if ($result) {
                Db::commit();
                return json(DefaultResponse(200, false, '更新成功!', null));
            }
            Db::rollback();
            return json(DefaultResponse(201, false, '更新失败!', null));
        } catch (ValidateException $e) {
            Db::rollback();
            return json(DefaultResponse(201, false, $e->getError(), null));
        } catch (\Exception $e) {
            Db::rollback();
            return json(DefaultResponse(201, false, '更新失败!', null));
        }
        Db::rollback();
        return json(DefaultResponse(200, false, '更新成功!', null));
    }

    /**
    * @desc    实时记录今天业绩
    * @date    2024/7/16
    * @author  zhangheng
    */
    public function export($params = [])
    {
        $header = [];
        $header[] = ['SPU', 'spu'];
        $header[] = ['采购总数', 'Purchases'];
        $header[] = ['采购在途', 'InTransit'];
        $header[] = ['采购到货', 'Delivery'];
        $header[] = ['已入库', 'Inventory'];
        $header[] = ['相差', 'PhaseDifference'];
        $header[] = ['更新时间', 'UpdateTime'];
        $tableName = "DailyQuantity";
        
        
        $spu     = $params['spu'] ?? '';
        $where = [];
        if($spu){
            $where[] = ['spu', 'like', '%' . $spu . '%'];
        }
        $lists = Db::name('feishu_daily_quantity')
            ->where($where)
            ->order('Inventory desc')
            ->select()->toarray();
        $fileName = "export_" . $tableName . "_" . time();
        return Excel::exportData($lists, $header, $fileName, 'xlsx');
    }
    
    

    public function info($post)
    {
        $spu = $post['spu'] ?? '';
        $where['spu'] = $spu;
        $limit = $params['limit'] ?? 10;
        $where = [];
        if($spu){
            $where[] = ['spu', 'like', '%' . $spu . '%'];
        }
        $list = Db::name('feishu_daily_quantityinfo')
            ->where($where)
            ->order('create_time desc')
            ->paginate($limit);

        $lists = $list->items();
        foreach ($lists as &$v) {
            if($v['create_time']){
                $v['create_time'] = date('Y-m-d', strtotime($v['create_time']));
            }else{
                $v['create_time'] = '';
            }
            if($v['delivery_time']){
                $v['delivery_time'] = date('Y-m-d', strtotime($v['delivery_time']));
            }else{
                $v['delivery_time'] = '';
            }
        }

        $data = [
            'list'  => $lists,
            'total' => $list->total(),
        ];
        
        return json(DefaultResponse(200, true, '获取成功', $data));
    }

}
